package cn.javaxc.model.page;
import java.sql.*;
import java.util.*;
import javax.servlet.http.*;

import cn.javaxc.mvcwork.framework.database.*;
import cn.javaxc.mvcwork.controller.*;
import cn.javaxc.mvcwork.controller.web.*;
import cn.javaxc.mvcwork.framework.actions.TActionContext;
import cn.javaxc.mvcwork.modules.login.UserBean;
import cn.javaxc.model.base.*;
import cn.javaxc.mvcwork.util.helper.*;
import cn.javaxc.mvcwork.util.common.*;
import cn.javaxc.mvcwork.modules.file.*;
import org.springframework.beans.factory.annotation.Autowired;
import cn.javaxc.mvcwork.util.helper.IBaseDAO;
import org.springframework.security.core.userdetails.UserDetails;
import com.ruoyi.common.core.domain.model.LoginUser;

public class VUEPage {
	public final static String PREFIXLIST="list_";
	public final static String mvc_page_list="pagination";
	public final static String SystemError="SystemError";
	public final static String PermissionError="PermissionError";
	public final static String ONLOAD="Include";
	public final static String ONSUBMIT="Submit";
	public final static String AJAXTREE="AjaxTree";
	public final static String AJAXLIST="AjaxList";
	public final static String AJAXCOMPLETE="AjaxComplete";
	public final static String AJAXOBJECT="AjaxObject";

	private boolean hasPermission=true;
	/**
	 * 包含上下文处理中的Request与Response对象
	 */
	protected TActionContext context=null;
	protected HttpServletRequest request;
//	protected SQLParam params;
	/**
	 * 分页信息
	 */
	protected PagedListInfo pagination=null;
	protected VUEFormBean hqlBean=new VUEFormBean();
	public void createPage(VUEPage _page){
//		setConnection(_page.db);
		setContext(_page.context);
		setPagination(_page.pagination);
	}

//	public void setConnection(Connection connection){
//		this.db=connection;
//	}
	public void setRequest(HttpServletRequest request){
		this.request=request;
	}
	public void setContext(TActionContext context){
		this.context=context;
		this.request=context.getRequest();
		this.hqlBean=(VUEFormBean)context.getFormBean();
	}
	public void setPagination(PagedListInfo page){
		this.pagination=page;
	}

	public boolean getHasPermission(){
		return this.hasPermission;
	}
	public void setHasPermission(boolean hasPermission){
		this.hasPermission=hasPermission;
	}
	public void buildPermission(Object hasperm){
		if (!Boolean.valueOf(String.valueOf(hasperm))){
			setHasPermission(false);
		}
	}
	protected boolean hasUserPermission(){
		UserDetails userSession = MSecurityUtils.getUserEntity();
		if (userSession != null && userSession.getUsername()!=null) {
			return true;
		}
		return false;
	}
	public SystemStatus getSystemStatus() {
		return AppUtils.getSystemStatus(context);
	}
	/**
	* 页面访问权限
	*/
	public void hasPermission()throws Exception{}
	public void onhead() throws Exception{

	}

	/**
	* 页面初始化数据
	*/
	public Object onload() throws Exception{
		return loadPage(getClass(),null);
	}

	public Object onloadsubmit() throws Exception{
		return ONSUBMIT;
	}

	/**
	* 处理页面提交
	*/
	public Object onsubmit() throws Exception{
		return ONSUBMIT;
	}
	/**
	* 页面表单校验
	*/
	public void onvalidate()throws Exception{}

	public String getReturn( String resultString) {
//		if ("ajaxList".equals(resultString)){
//			if ("true".equals(request.getParameter("ajax"))){
//				return "AjaxList";	
//			}else{
//				return "HtmlList";
//			}		
//		}
		return resultString;
	}

	
	public static boolean validateEqlBean(VUEFormBean emtMap){
		if(emtMap!=null){
		  if (emtMap.getErrList().size() > 0) {
			return false;
		  }
		}
		return true;
	}
	public void doValidatePermission(String permissionName){
		context.setAttribute("mvc_permission",permissionName);
		LoginUser thisUser = MSecurityUtils.getUserEntity();
		if (thisUser != null) {
			if (thisUser.getPermissions().contains(permissionName)){
				return;
			}
		}
		setHasPermission(false);
	}
//    public SQLList createList(){
//		return EQL.createList();
//    }
//	public SQLMap createElement(){
//		return EQL.createElement();
//	}	
	public String getParameter(String param){
		return request.getParameter(param);
	}

	public Object onUploadFile()throws Exception{
		VUEPageFile filePage=MVCConfig.getRequestHelper().createFilePage(); 
		filePage.setPage(this);
		String result=filePage.doInsertFile(false);
		if (filePage.hasError()){
			return result;
		}
		return onsubmit();		
	}
	public Object onInsertFile()throws Exception{
		VUEPageFile filePage=MVCConfig.getRequestHelper().createFilePage(); 
		filePage.setPage(this);
		String result=filePage.doInsertFile(true);
		if (filePage.hasError()){
			return result;
		}
		return onsubmit();		
	}
	public Object onUpdateFile()throws Exception{
		  Object fileId = CommonUtil.parseInt(RequestHelper.getRequestValue(request,"fileid"),-1);
		  int fileverid = CommonUtil.parseInt(RequestHelper.getRequestValue(request,"fileverid")+"",-1);
		  double version = CommonUtil.parseDouble(RequestHelper.getRequestValue(request,"version"),1.0);

		
		if (fileverid>0){
			FileItemVersion ver=new FileItemVersion();
			ver.queryRecord(fileverid,context);
			fileId=ver.getItemId();
			version=ver.getVersion();
			
		}
		if (!StringUtils.isNullData(fileId)&&version>0){

			FileItem thisItem = new FileItem(fileId);
			thisItem.buildVersionList();

			FileItemVersion itemVersion = thisItem.getVersion(version);
			if (itemVersion!=null){
				itemVersion.delete(AppUtils.getModuleFileLibrary(context,thisItem.getLinkModuleId()));
			}
			
			//////
			request.setAttribute("version",version+"");
			request.setAttribute("fileid",fileId);
		}
		return onInsertFile();		
	}
    public String onSearch() throws Exception{

	   if("true".equals(request.getParameter("searchClear")))pagination.getMap().clear();
	   Enumeration e = request.getParameterNames();
	   while (e.hasMoreElements()){
		String name = e.nextElement().toString();		
		if(request.getQueryString().indexOf(name+"=") ==-1) {
			pagination.getMap().put(name,request.getParameter(name));
		}
	   }
    return "Submit";
	
  }
//  protected Connection getConnection() throws SQLException {
//
//	return null;
//  }
//  protected void freeConnection() {
//  }
//
//  public void beginTransaction(boolean istran) throws SQLException{
//	m_db = this.getConnection();
//    if (m_db != null&&istran){
//		m_db.setAutoCommit(false);    
//    }
//  }
//  public void endTransaction(boolean istran) throws SQLException{
//	if (m_db != null&&istran) {
//	  m_db.setAutoCommit(true);
//	}
//	freeConnection();
//  }
//  public void rollback() throws SQLException{
//	if (m_db != null&&!m_db.getAutoCommit()) {
//	  m_db.rollback();			
//	}else{
//		org.springframework.transaction.interceptor.TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
//	}
//
//  }
//  public void commit() throws SQLException{
//	if (m_db != null&&!m_db.getAutoCommit()) {
//		m_db.commit();
//	}
//  }
  public String sourcePage(Class clazz,Map m) {
	return sourcePage("/"+clazz.getName().replace(".","/"),m);
  }
  public String sourcePage(String clazz,Map m) {
	mapFill(m);
	context.setAttribute("include",clazz);
	return ONSUBMIT;
  }
  public String loadPage(Class clazz,Map m) {
	return loadPage("/"+clazz.getName().replace("cn.javaxc.","").replace(".","/").replaceFirst("/action/","/"),m);
  }
  public String loadPage(String clazz,Map m) {
	mapFill(m);
	context.setAttribute("include",clazz);
	if ("true".equals(context.getConfig().get("layout.pagetext","false"))){
		context.setAttribute("__mvctext__",FrameHelper.getBaseDAO().getpagetext(clazz));
	}
	AppUtils.addFormElements(context);
    MVCConfig.getRequestHelper().createTParams(context.getRequest());
	return ONLOAD;
  }
  public void mapFill(Map<String,Object> m) {
	if(m==null)return;
	for (String k:m.keySet()){
		if (m.get(k)!=null){
			request.setAttribute(k,m.get(k));
		}
	}
  }
}
